/**
 * Created by leiqr on 2015/6/8.
 * 运营中心首页
 */
define(function (require, exports, module) {
    var $ = require("jquery");
    var template = require("template");
    require('jqueryPagination');
    var tip = require('js/tip');
    var api_prefix = "http://120.24.100.98:9999/"; //api前缀
    var main = {
        init: function () {
            var me = this;
            var today = new Date();

            function getUrl(today) {
                return 'http://jira.mysoft.com.cn/issues/?jql=project = YKCPXQ AND fixVersion = ' + today.getFullYear() + '年' + (today.getMonth() + 1) + '月需求 ORDER BY cf[10235] DESC, priority DESC';
            }

            //获取当月需求链接
            $("#cur_month").attr('href', getUrl(today));
            //获取下月需求链接
            today.setMonth(today.getMonth() + 1);
            $("#next_month").attr('href', getUrl(today));
            if (location.pathname === '/log/product_updates_log.html') {
                //获取云客产品更新日志
                main.releaseProjectLogSearch();
            } else {
                //需求进展列表数据
                main.unreleaseProjectLogSearch();
            }
            main.createSearch();
            main.getProdList();
            main.bindEvents();
        },
        bindEvents: function () {
            $(".project-list").on('mouseenter', 'p a span.project-name', function () {
                if ($.trim($(this).find(".desc-box").text())) {
                    $(this).find(".desc-box").show();
                }
            }).on('mouseleave', 'p a span.project-name', function () {
                $(this).find(".desc-box").fadeOut(100);
                clearSelectedState();
            }).on('click', 'p a', function (e) {
                var url = $(this).data("url");
                if (url) {
                    window.open(url);
                }
            }).on('click', 'p a span.project-name em', function (e) {
                e.stopPropagation();
            });

            $(document).on('click', '.log-search-btn', function (e) {
                if (location.pathname === '/log/product_updates_log.html') {
                    main.releaseProjectLogSearch($('#log-select').val(), $('#log-keyword').val(), 1, 20);
                } else {
                    main.unreleaseProjectLogSearch($('#log-select').val(), $('#log-keyword').val(), 1, 20);
                }
            }).on('keydown', '#log-keyword', function (e) {
                if(e.keyCode === 13) {
                    $('.log-search-btn').click();
                }
            }).on('change', '#log-select', function(e) {
                main.curpage = 0;
                $('#log-keyword')[0] && $('#log-keyword')[0].focus();
                $('.log-search-btn').click();
            });

            $('#navbar').click(function (e) {
                var target = $(e.target);
                var aid = target.data('aid') || target.parent().data('aid');
                if (aid !== undefined) {
                    var pid = target.data('pid') || target.parent().data('pid');
                    location.href = "/list.html?dirId=" + aid + "&pid=" + pid;
                }
            });

            function clearSelectedState() {
                "getSelection" in window
                    ? window.getSelection().removeAllRanges()
                    : document.selection.empty();
            }
        },
        /**
         * 云客产品更新日志
         * @param pageIndex
         * @param pageSize
         * @returns {JQueryPromise<T>}
         */
        getReleaseProject: function (pageIndex, pageSize) {
            pageSize = pageSize || 20;
            var setting = {
                type: "get",
                url: api_prefix + "redmine.php?m=getReleaseProject",
                dataType: "jsonp",
                jsonp: "callback",
                data: {pageIndex: pageIndex, pageSize: pageSize},
                success: function (res) {
                    var retCode = res.retCode;
                    if (retCode == 0) {
                        $('#Readmin_list').html(template('Readmin_template', {ReadminList: res.data.items}));
                        main.createPaging(res.data.total, pageSize, function (index) {
                            main.getReleaseProject(index + 1)
                        });
                    } else {
                        tip.showError(res.errMsg);
                    }
                },
                error: function () {
                    tip.showError("系统错误，请稍后再试");
                }
            };
            return $.ajax(setting);

        },
        /**
         * 需求进展列表数据
         * @param pageIndex
         * @param pageSize
         * @returns {JQueryPromise<T>}
         */
        getUnreleaseProject: function (pageIndex, pageSize) {
            pageSize = pageSize || 20;
            var setting = {
                type: "get",
                url: api_prefix + "redmine.php?m=getUnreleaseProject",
                dataType: "jsonp",
                jsonp: "callback",
                data: {pageIndex: pageIndex, pageSize: pageSize},
                success: function (res) {
                    var retCode = res.retCode;
                    if (retCode == 0) {
                        $('#Readmin_list').html(template('Readmin_template', {ReadminList: res.data.items}));
                        main.createPaging(res.data.total, pageSize, function (index) {
                            main.getUnreleaseProject(index + 1)
                        });
                    } else {
                        tip.showError(res.errMsg);
                    }
                },
                error: function () {
                    tip.showError("系统错误，请稍后再试");
                }
            };
            $.ajax(setting);
        },
        /**
         * 需求进展列表数据分页
         * @param len 列表记录总数
         * @returns {JQueryPromise<T>}
         */
        createPaging: function (len, pageSize, callback) {
            var self = this;
            $("#pagination").pagination(len, {
                num_edge_entries: 1, //边缘页数
                num_display_entries: 4, //主体页数
                current_page: self.curpage,
                callback: function (index) {
                    self.curpage = index - 1;
                    callback && callback(self.curpage);
                },
                items_per_page: pageSize, //每页显示1项
                prev_text: "前一页",
                next_text: "后一页"
            });
        },
        createSearch: function (data) {
            $('.log-search').html(template('log_search_template', data || {prods: []}));
        },
        getProdList: function () {
            return main.getJsonp("redmine.php?m=getProdList", function (res) {
                var retCode = res.retCode;
                if (retCode == 0) {
                    main.createSearch({prods: res.data});
                } else {
                    tip.showError(res.errMsg);
                }
            });
        },
        releaseProjectLogSearch: function (prodId, keyword, pageIndex, pageSize) {
            pageSize = pageSize || 20;
            return main.getJsonp("redmine.php?m=getReleaseProject", function (res) {
                var retCode = res.retCode;
                if (retCode == 0) {
                    $('#Readmin_list').html(template('Readmin_template', {ReadminList: res.data.items}));
                    main.createPaging(res.data.total, pageSize, function (index) {
                        main.releaseProjectLogSearch(prodId, keyword, index + 1)
                    });
                } else {
                    tip.showError(res.errMsg);
                }
            }, {pageIndex: pageIndex || 1, pageSize: pageSize, keyword: keyword, prodId: prodId});
        },
        unreleaseProjectLogSearch: function (prodId, keyword, pageIndex, pageSize) {
            pageSize = pageSize || 20;
            return main.getJsonp("redmine.php?m=getUnreleaseProject", function (res) {
                var retCode = res.retCode;
                if (retCode == 0) {
                    $('#Readmin_list').html(template('Readmin_template', {ReadminList: res.data.items}));
                    main.createPaging(res.data.total, pageSize, function (index) {
                        main.unreleaseProjectLogSearch(prodId, keyword, index + 1)
                    });
                } else {
                    tip.showError(res.errMsg);
                }
            }, {pageIndex: pageIndex || 1, pageSize: pageSize, keyword: keyword, prodId: prodId});
        },
        getJsonp: function (url, success, data) {
            return $.ajax({
                type: "get",
                url: api_prefix + url,
                dataType: "jsonp",
                jsonp: "callback",
                data: data,
                success: function (res) {
                    if (typeof success == 'function') {
                        success(res)
                    }
                },
                error: function () {
                    tip.showError("系统错误，请稍后再试");
                }
            });
        }
    };
    main.init();
})
;







